﻿package yii.db.ar{

	import yii.db.schema.*;
	import phpas3.*;

	public class CJoinQuery{


	/**
	 * @var array list of column selections
	 */
	public var selects:Array=new Array();
	/**
	 * @var boolean whether to select distinct result set
	 * @since 1+0+9
	 */
	public var distinct:Boolean=false;
	/**
	 * @var array list of join statement
	 */
	public var joins:Array=new Array();
	/**
	 * @var array list of WHERE clauses
	 */
	public var conditions:Array=new Array();
	/**
	 * @var array list of ORDER BY clauses
	 */
	public var orders:Array =new Array();
	/**
	 * @var array list of GROUP BY clauses
	 */
	public var groups:Array =new Array();
	/**
	 * @var array list of HAVING clauses
	 */
	public var havings:Array =new Array();
	/**
	 * @var integer row limit
	 */
	public var limit:int=-1;
	/**
	 * @var integer row offset
	 */
	public var offset:int=-1;
	/**
	 * @var array list of query parameters
	 */
	public var params:Array =new Array();
	/**
	 * @var array list of join element IDs (id=>true)
	 */
	public var elements:Array =new Array();




	/**
	 * Joins with another join element
	 * @param CJoinElement $element the element to be joined
	 */
	public function join( $element:CJoinElement ):void{
		if($element.slave!==null)
			this.join($element.slave);
		if(!empty($element.relation.select))
			this.selects.push( $element.getColumnSelect($element.relation.select) );
		this.conditions.push( $element.relation.condition );
		this.orders.push( $element.relation.order );
		this.joins.push( $element.getJoinCondition() );
		this.joins.push( $element.relation.join );
		this.groups.push( $element.relation.group );
		this.havings.push( $element.relation.having );

		if(is_array($element.relation.params)){
			if(is_array(this.params))
				this.params=array_merge(this.params,$element.relation.params);
			else
				this.params=$element.relation.params;
		}
		this.elements[$element.id]=true;
	}




	/**
	 * Constructor+
	 * @param CJoinElement $joinElement The root join tree+
	 * @param CDbCriteria $criteria the query criteria
	 */
	public function CJoinQuery( $joinElement:CJoinElement, $criteria:CDbCriteria=null ){
		if($criteria!==null){
			this.selects.push( $joinElement.getColumnSelect($criteria.select) );
			this.joins.push( $joinElement.getTableNameWithAlias() );
			this.joins.push( $criteria.join );
			this.conditions.push( $criteria.condition );
			this.orders.push( $criteria.order );
			this.groups.push( $criteria.group );
			this.havings.push( $criteria.having );
			this.limit=$criteria.limit;
			this.offset=$criteria.offset;
			this.params=$criteria.params;
			if(!this.distinct && $criteria.distinct)
				this.distinct=true;
		} else {
			this.selects.push( $joinElement.getPrimaryKeySelect() );
			this.joins.push( $joinElement.getTableNameWithAlias() );
			this.conditions.push( $joinElement.getPrimaryKeyRange() );
		}
		this.elements[$joinElement.id]=true;
	}







	}
}